﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using Telerik.Web.UI;
using BSCMFF.Core.DataAccessLayer;
using BSCMFF.Web.UserControls.Ratings;

namespace BSCMFF.Web.Pages
{
    public partial class KPIListMoore : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {

        }

        protected void RadGrid1_ItemDataBound(object sender, GridItemEventArgs e)
        {
            if (e.Item is GridDataItem)
            {
                GridDataItem gridItem = e.Item as GridDataItem;
                foreach (GridColumn column in RadGrid1.MasterTableView.RenderColumns)
                {
                    if (column is GridBoundColumn)
                    {
                        //this line will show a tooltip based on the CustomerID data field
                        //TODO: solution instead of target .. gridItem[column.UniqueName].ToolTip = ((KPI)e.Item.DataItem).Strategic_Target.Description;// "CustomerID: " + gridItem.OwnerTableView.DataKeyValues[gridItem.ItemIndex]["KPI_ID"].ToString();
                    }
                }
            }
        }

        protected void RadGrid1_InsertCommand(object source, GridCommandEventArgs e)
        {
            GridEditableItem newItem = e.Item as GridEditableItem;
            UserControl form = (UserControl)newItem.FindControl(GridEditFormItem.EditFormUserControlID);

            KPI kpi = new KPI();

            kpi.Name = (form.FindControl("TextBoxName") as RadTextBox).Text;
            kpi.Description = (form.FindControl("TextBoxDescription") as RadTextBox).Text;
            //TODO : Solution//kpi.Strategic_Target_FK = Convert.ToInt32((form.FindControl("ComboBoxStrategicTarget") as RadComboBox).SelectedValue);
            
            //kpi.Weight = Convert.ToInt32((form.FindControl("NumericTextBoxWeight") as RadNumericTextBox).Value);
            //kpi.Start_Value = Convert.ToInt32((form.FindControl("NumericTextBoxStartValue") as RadNumericTextBox).Value);
            //kpi.Current_Value = Convert.ToInt32((form.FindControl("NumericTextBoxCurrentValue") as RadNumericTextBox).Value);
            //kpi.Target_Value = Convert.ToInt32((form.FindControl("NumericTextBoxTargetValue") as RadNumericTextBox).Value);
            //kpi.Target_Description = (form.FindControl("TextBoxTargetDescription") as RadTextBox).Text;

            kpi.Priority = Convert.ToInt32((form.FindControl("RBPriority") as RatingBar).DbValue);


            BSCEntities context = new BSCEntities();
            context.KPIs.AddObject(kpi);
            context.SaveChanges();
        }

        protected void RadGrid1_DeleteCommand(object source, GridCommandEventArgs e)
        {
            GridEditableItem deleteItem = e.Item as GridEditableItem;
            int kpi_ID = Convert.ToInt32(deleteItem.OwnerTableView.DataKeyValues[deleteItem.ItemIndex]["KPI_ID"]);

            BSCEntities context = new BSCEntities();

            KPI kpi = (from k in context.KPIs
                       where k.KPI_ID == kpi_ID
                       select k).Single();

            context.KPIs.DeleteObject(kpi);
            context.SaveChanges();
        }

        protected void RadGrid1_UpdateCommand(object source, GridCommandEventArgs e)
        {
            GridEditableItem editedItem = e.Item as GridEditableItem;
            UserControl form = (UserControl)editedItem.FindControl(GridEditFormItem.EditFormUserControlID);

            BSCEntities context = new BSCEntities();
            int kpi_ID = Convert.ToInt32(editedItem.OwnerTableView.DataKeyValues[editedItem.ItemIndex]["KPI_ID"]);
            KPI kpi = (from k in context.KPIs
                       where k.KPI_ID == kpi_ID
                       select k).Single();

            kpi.Name = (form.FindControl("TextBoxName") as RadTextBox).Text;
            kpi.Description = (form.FindControl("TextBoxDescription") as RadTextBox).Text;
            //TODO: solution //kpi.Strategic_Target_FK = Convert.ToInt32((form.FindControl("ComboBoxStrategicTarget") as RadComboBox).SelectedValue);
            //kpi.Weight = Convert.ToInt32((form.FindControl("NumericTextBoxWeight") as RadNumericTextBox).Value);
            //kpi.Start_Value = Convert.ToInt32((form.FindControl("NumericTextBoxStartValue") as RadNumericTextBox).Value);
            //kpi.Current_Value = Convert.ToInt32((form.FindControl("NumericTextBoxCurrentValue") as RadNumericTextBox).Value);
            //kpi.Target_Value = Convert.ToInt32((form.FindControl("NumericTextBoxTargetValue") as RadNumericTextBox).Value);
            //kpi.Target_Description = (form.FindControl("TextBoxTargetDescription") as RadTextBox).Text;
            kpi.Priority = Convert.ToInt32((form.FindControl("RBPriority") as RatingBar).DbValue);

            context.SaveChanges();
        }

    }
}